package com.wudizaba.nb;

import com.wudizaba.nb.domain.Student;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * stream流常用方法
 *
 * @author wudizaba
 */
@SpringBootTest
@Slf4j
public class JavaSeTestStream {

    @Test
    void getStream() {
        System.out.println("stream流常用方法 + 集合常用方法");
        Integer[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        List<Integer> list = new ArrayList<>(100);
        list.addAll(List.of(arr));
        //获取流
        Stream<Integer> stream = list.stream();
        Stream<List<Integer>> stream1 = Stream.of(list);
//        IntStream stream2 = Arrays.stream(arr);


        //1.forEach（终结方法）循环
        System.out.println("forEach（终结方法）循环");
        list.stream().forEach((s) -> System.out.println(s.toString()));
        System.out.println("===========================这是分割线=============================");


        //2.过滤：filter 保留符合条件的值
        System.out.println("filter 保留符合条件的值");
        list.stream().filter(integer -> integer == 5).forEach(System.out::println);
        System.out.println("===========================这是分割线=============================");


        List<Student> studentList = getStudentList();
        //3.映射(转换)：map获取一条数据 + collect收集器 + Collectors转换成集合
        System.out.println("map获取一条数据 + collect收集器 + Collectors转换成集合");
        studentList.stream().map(s -> s.getName()).collect(Collectors.toSet()).forEach(System.out::println);
        System.out.println("===========================这是分割线=============================");

        //转换成字符串，并用指定符号拼接
        System.out.println("转换成字符串，并用指定符号拼接");
        String collect = studentList.stream().map(s -> s.getName()).collect(Collectors.joining(","));
        System.out.println("用指定字符拼接后的数据：" + collect);
        System.out.println("===========================这是分割线=============================");


        //4.统计个数：count（终结方法）
        System.out.println("统计个数：count（终结方法）");
        long count = studentList.stream().count();
        System.out.println("统计对象数量" + count);
        System.out.println("===========================这是分割线=============================");


        //5.取用前几个（截取）：limit
        System.out.println("取用前几个（截取）：limit");
        studentList.stream().limit(5).forEach(System.out::println);
        System.out.println("===========================这是分割线=============================");


        //6.跳过前几个元素：skip
        System.out.println("跳过前几个元素：skip");
        studentList.stream().skip(5).forEach(System.out::println);
        System.out.println("===========================这是分割线=============================");

        //7.组合（合并流）：concat
        System.out.println("组合（合并流）：concat");
        Stream<Integer> stream3 = Stream.of(1, 2, 3, 4, 5);
        Stream<Integer> stream4 = Stream.of(6, 7, 8, 9, 10);
        Stream<Integer> stream5 = Stream.concat(stream3, stream4);
        stream5.forEach(System.out::println);
        System.out.println("===========================这是分割线=============================");

        //8.筛选：distinct
        System.out.println("去重：distinct");
        studentList.stream().distinct().forEach(System.out::println);
        System.out.println("===========================这是分割线=============================");

        //9.映射(打开后再转换)：flatMap
        System.out.println("映射(打开后再转换)：flatMap");
        studentList.stream().flatMap(s -> Stream.of(s.getName())).forEach(System.out::println);
        System.out.println("===========================这是分割线=============================");

        //10.排序：sorted
        System.out.println("排序 ：sorted");
        studentList.stream().sorted((s1, s2) -> s1.getId().compareTo(s2.getId())).forEach(System.out::println);
        System.out.println("===========================这是分割线=============================");


        //12.检测匹配（终结方法）：
        //返回一个Boolean值
        //
        //是否全部匹配：allMatch
        //
        //是否至少匹配一个：anyMatch
        //
        //是否没有匹配的：noneMatch
        System.out.println("检测匹配（终结方法）：//返回一个Boolean值\n" +
                "        //\n" +
                "        //是否全部匹配：allMatch\n" +
                "        //\n" +
                "        //是否至少匹配一个：anyMatch\n" +
                "        //\n" +
                "        //是否没有匹配的：noneMatch");
        boolean b = studentList.stream().allMatch(s -> s.getId().equals("1"));
        if (b) {
            System.out.println("全部匹配");
        }

        boolean b1 = studentList.stream().anyMatch(s -> s.getId().equals("1"));
        if (b1) {
            System.out.println("至少匹配一个");
        }

        boolean b2 = studentList.stream().noneMatch(s -> s.getId().equals("1"));
        if (b2) {
            System.out.println("没有匹配的");
        }
        System.out.println("===========================这是分割线=============================");


        //13.查找元素（终结方法）
        //查找第一个元素：findFirst，返回Optional类型 ,ifPresent
        //
        //查找其中一个元素：findAny，返回Optional类型
        System.out.println("查找元素（终结方法）\n" +
                "        //查找第一个元素：findFirst，返回Optional类型 ,ifPresent\n" +
                "        //\n" +
                "        //查找其中一个元素：findAny，返回Optional类型");
        Optional<Student> first = studentList.stream().findFirst();
        System.out.println(first);

        Optional<Student> any = studentList.stream().findAny();
        System.out.println(any);
        System.out.println("===========================这是分割线=============================");

        //14.查找最大最小值（终结方法）
        //max(comparator c)
        //
        //min(comparator c)
        System.out.println("//14.查找最大最小值（终结方法）\n" +
                "        //max(comparator c)\n" +
                "        //\n" +
                "        //min(comparator c)");
        Optional<Integer> max = list.stream().max((s1, s2) -> s1.compareTo(s2));
        System.out.println("最大值：" + max);

        Optional<Integer> min = list.stream().min((s1, s2) -> s1.compareTo(s2));
        System.out.println("最小值：" + min);
        System.out.println("===========================这是分割线=============================");



    }

    @Test
    void Collection() {

    }

    public List<Student> getStudentList() {
        Student student1 = new Student("张三", "1", "男", "18", "123456789", "123456789", "123456789", "12345", "123456789", "123456789");
        Student student2 = new Student("张三", "100", "男", "18", "123456789", "123456789", "123456789", "12345", "123456789", "123456789");
        Student student3 = new Student("张三", "19", "男", "18", "123456789", "123456789", "123456789", "12345", "123456789", "123456789");
        Student student4 = new Student("张三", "15", "男", "18", "123456789", "123456789", "123456789", "12345", "123456789", "123456789");
        Student student5 = new Student("张三", "40", "男", "18", "123456789", "123456789", "123456789", "12345", "123456789", "123456789");
        Student student6 = new Student("张三", "10", "男", "18", "123456789", "123456789", "123456789", "12345", "123456789", "123456789");
        Student student7 = new Student("张三", "300", "男", "18", "123456789", "123456789", "123456789", "12345", "123456789", "123456789");
        Student student8 = new Student("张三", "10", "男", "18", "123456789", "123456789", "123456789", "12345", "123456789", "123456789");
        Student student9 = new Student("张三", "2", "男", "18", "123456789", "123456789", "123456789", "12345", "123456789", "123456789");

        List<Student> studentList = new ArrayList<>();
        studentList.add(student1);
        studentList.add(student2);
        studentList.add(student3);
        studentList.add(student4);
        studentList.add(student5);
        studentList.add(student6);
        studentList.add(student7);
        studentList.add(student8);
        studentList.add(student9);
        return studentList;
    }
}
